package com.ymttest.database.sqlwapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtUsers;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name : YmtUserWapper.java
 * @Author : zhouyi
 * @Date : 2015年4月2日
 * @Copyright : 洋码头
 ************************************************************************************/
public class YmtUsersWapper {
	SqlManager manage;

	public YmtUsersWapper() {
		manage = new SqlManager("ymt" + EnvSetup.getEnv());
	}

	/**
	 * 使用登入id获取UserId
	 * 
	 * @param loginid
	 *            登入id
	 * @return UserId,如果没有返回null
	 * @throws InterruptedException
	 */
	public String getUserId(String loginid) throws InterruptedException {
		String userid = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("loginid", loginid);
		Thread.sleep(1000);
		List<YmtUsers> userList = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectBySLoginid", map);
		if (userList.size() > 0) {
			userid = userList.get(0).getIuserid().toString();
		}
		// manage.close();
		return userid;
	}

	/**
	 * 获取最大的UserId
	 */
	public int getMaxUserId() {
		int MaxUserId = 0;
		Map<?, ?> map = new HashMap();
		List<YmtUsers> userList = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectMaxUserId", map);
		if (userList.size() > 0) {
			MaxUserId = userList.get(0).getIuserid();
		}
		return MaxUserId;
	}

	/**
	 * 使用userid 获取YmtUsers
	 * 
	 * @param userid
	 * @return YmtUsers
	 * @throws InterruptedException
	 */
	public YmtUsers getYmtUsers(int userid) throws InterruptedException {
		Map<String, String> map = new HashMap<String, String>();
		YmtUsers users = new YmtUsers();
		Thread.sleep(1000);
		List<YmtUsers> userList = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectByPrimaryKey", userid);
		if (userList.size() > 0) {
			users = userList.get(0);
		}
		// manage.close();
		return users;
	}

	/**
	 * 使用loginId 获取YmtUsers
	 * 
	 * @param userid
	 * @return YmtUsers
	 * @throws InterruptedException
	 */
	public YmtUsers getYmtUsers(String loginId) throws InterruptedException {
		Map<String, String> map = new HashMap<String, String>();
		YmtUsers users = new YmtUsers();
		Thread.sleep(1000);
		map.put("loginid", loginId);
		Thread.sleep(1000);
		List<YmtUsers> userList = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectBySLoginid", map);
		if (userList.size() > 0) {
			users = userList.get(0);
		}
		return users;
	}

	/**
	 * 创建用户
	 */
	@SuppressWarnings("rawtypes")
	public void createUser(YmtUsers user) {
		int res = manage.insert("com.ymttest.database.mapping.YmtUsersMapper.insert", user);
		manage.commit();
	}

	/**
	 * 使用登入id获取UserId
	 * 
	 * @param userid
	 *            登入id
	 * @return UserId,如果没有返回null
	 * @throws InterruptedException
	 */
	public String getLoginId(int userid) {
		String loginid = null;
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		List<YmtUsers> userList = manage.getSelectResults("com.ymttest.database.mapping.YmtUsersMapper.selectByUid",
				userid);
		if (userList.size() > 0) {
			loginid = userList.get(0).getSloginid().toString();
		}
		// manage.close();
		return loginid;
	}

	/**
	 * 强制更新uid支付密码为abc123
	 * 
	 * @param UserId
	 * @return
	 */
	public int updateTradingPwdByUid(int UserId) {
		HashMap m = new HashMap();
		m.put("pwd", "54E12B6B392FD98136162FE3F7343AD5");
		m.put("uid", UserId);
		int result = manage.update("com.ymttest.database.mapping.YmtUsersMapper.updateTradingPwdByUid", m);
		manage.commit();
		return result;
	}

	/**
	 * 根据用户Id查找出微信用户
	 * 
	 * @param userId
	 * @return
	 */
	public Map selectUserAtWeixinByUserId(int userId) {
		HashMap m = new HashMap();
		m.put("userId", userId);
		List<Map> users = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectUserAtWeixinByUserId", m);
		manage.commit();
		if (users.size() == 1) {
			Map user = users.get(0);
			return user;
		} else
			return null;
	}

	/**
	 * 查找最后一个微信用户
	 * 
	 * @return
	 */
	public Map selectLastWeixin() {
		List<Map> users = manage.getSelectResults("com.ymttest.database.mapping.YmtUsersMapper.selectLastWeixin", null);
		manage.commit();
		if (users.size() == 1) {
			Map user = users.get(0);
			return user;
		} else
			return null;
	}

	/**
	 * 根据微信OpenID查找微信用户
	 * 
	 * @param openId
	 * @return
	 */
	public Map selectUserAtWeiXinByOpenId(String openId) {
		HashMap m = new HashMap();
		m.put("openId", openId);
		List<Map> users = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectUserAtWeiXinByOpenId", m);
		manage.commit();
		if (users.size() == 1) {
			Map user = users.get(0);
			return user;
		} else
			return null;
	}

	/**
	 * 根据userId查找第三方用户
	 * 
	 * @param userId
	 * @return
	 */
	public Map selectThirdUserByYmtUserId(String table, String col, int userId) {
		HashMap m = new HashMap();
		m.put("table", table);
		m.put("col", col);
		m.put("userId", userId);
		List<Map> users = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectThirdUserByYmtUserId", m);
		manage.commit();
		if (users.size() == 1) {
			Map user = users.get(0);
			return user;
		} else
			return null;
	}

	/**
	 * 根据ali用户Id查询用户
	 * 
	 * @param aliUserId
	 * @return
	 */
	public Map selectUserAtAliByAliId(String aliUserId) {
		HashMap m = new HashMap();
		m.put("aliUserId", aliUserId);
		List<Map> users = manage.getSelectResults("com.ymttest.database.mapping.YmtUsersMapper.selectUserAtAliByAliId",
				m);
		manage.commit();
		if (users.size() >= 1) {
			Map user = users.get(0);
			return user;
		} else
			return null;
	}

	/**
	 * 随机选择一个绑定过手机号的第三方账号
	 * 
	 * @param table
	 * @return 用户Id, 手机号
	 */
	public Map selectThirdPartyBindPhone(String ymtUserIdCol, String thirdPartyUserIdCol, String table) {
		Map param = new HashMap<String, Object>();
		param.put("ymtUserId", ymtUserIdCol);
		param.put("thirdPartyUserId", thirdPartyUserIdCol);
		param.put("table", table);
		List<Map> user = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectThirdPartyBindPhone", param);
		manage.commit();
		if (user.size() == 1) {
			return user.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 随机选择一个未绑定过手机号的第三方账号
	 * 
	 * @param table
	 * @return 用户Id, 手机号
	 */
	public Map getThirdPartyNotBindPhone(String table, String ymtUserIdCol) {
		Map param = new HashMap<String, Object>();
		param.put("ymtUserId", ymtUserIdCol);
		param.put("table", table);
		List<Map> user = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.getThirdPartyNotBindPhone", param);
		manage.commit();
		if (user.size() == 1) {
			return user.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 选择一个绑定过其中一个第三方账号的手机
	 * 
	 * @param ymtUserIdCol
	 * @param thirdPartyUserIdCol
	 * @param table
	 * @return
	 */
	public Map selectOneThirdPartyBindPhone(String ymtUserIdCol, String thirdPartyUserIdCol, String table) {
		Map param = new HashMap<String, Object>();
		param.put("ymtUserId", ymtUserIdCol);
		param.put("thirdPartyUserId", thirdPartyUserIdCol);
		param.put("table", table);
		
		List<Map> user = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectThirdPartyBindPhone", param);
		manage.commit();
		if (user.size() == 1) {
			return user.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 随机获取一个绑定了洋码头账号的第三方账号
	 * 
	 * @param table
	 *            动态指定表（微博，微信，阿里）
	 * @return
	 */
	public Map selectRandomThirdPartyBindYmt(String table, String notNullcol) {
		Map param = new HashMap<String, Object>();
		param.put("table", table);
		param.put("notNullcol", notNullcol);
		List<Map> user = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectRandomThirdPartyBindYmt", param);
		manage.commit();
		if (user.size() == 1) {
			return user.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 根据第三方账号Id获取第三方信息
	 * 
	 * @param table
	 * @param col
	 * @param thirdPartyId
	 * @return
	 */
	public Map getThirdPartyByThirdPartyId(String table, String col, String thirdPartyId) {
		Map param = new HashMap<String, Object>();
		param.put("table", table);
		param.put("col", col);
		param.put("thirdPartyId", thirdPartyId);
		List<Map> user = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.getThirdPartyByThirdPartyId", param);
		manage.commit();
		if (user.size() == 1) {
			return user.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 获取只绑定特定的第三方账号的用户
	 * 
	 * select top 1 a.* from ${targetTable} a where a.${targetCol} ${condition1}
	 * (select b.${thirdPartyCol1} from ${thirdPartyTable1} b where
	 * b.${thirdPartyCol1} !=null) and a.${targetCol} ${condition2} (select
	 * c.${thirdPartyCol2} from ${thirdPartyTable2} c where c.${thirdPartyCol2}
	 * !=null)
	 * 
	 * @return
	 */
	public List<Map> selectThridPartyBindYmtByCondition(String targetTable, String targetCol, String condition1,
			String thirdPartyCol1, String thirdPartyTable1, String condition2, String thirdPartyCol2,
			String thirdPartyTable2) {
		Map param = new HashMap<String, Object>();
		param.put("targetTable", targetTable);
		param.put("targetCol", targetCol);
		param.put("condition1", condition1);
		param.put("thirdPartyCol1", thirdPartyCol1);
		param.put("thirdPartyTable1", thirdPartyTable1);
		param.put("condition2", condition2);
		param.put("thirdPartyCol2", thirdPartyCol2);
		param.put("thirdPartyTable2", thirdPartyTable2);
		List<Map> user = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectThridPartyBindYmtByCondition", param);
		manage.commit();
		return user;
	}

	public List<Map> selectThridPartyBindYmt(String targetTable, String targetCol, String condition1,
			String thirdPartyCol1, String thirdPartyTable1, String condition2, String thirdPartyCol2,
			String thirdPartyTable2) {
		Map param = new HashMap<String, Object>();
		param.put("targetTable", targetTable);
		param.put("targetCol", targetCol);
		param.put("condition1", condition1);
		param.put("thirdPartyCol1", thirdPartyCol1);
		param.put("thirdPartyTable1", thirdPartyTable1);
		param.put("condition2", condition2);
		param.put("thirdPartyCol2", thirdPartyCol2);
		param.put("thirdPartyTable2", thirdPartyTable2);
		List<Map> user = manage.getSelectResults("com.ymttest.database.mapping.YmtUsersMapper.selectThridPartyBindYmt",
				param);
		manage.commit();
		return user;
	}

	/**
	 * 获取绑定过手机的第三方账号 SELECT top 10 * from Ymt_User_At_SinaWeibo where
	 * YmatouUserId in (select iUserId from Ymt_MobilePhoneAccount where iUserId
	 * is not null and sAccountName is not null);
	 * 
	 * @param table
	 * @return
	 */
	public Map selectThirdPartyBindPhone(String table, String col) {
		Map param = new HashMap<String, Object>();
		param.put("table", table);
		param.put("col", col);
		List<Map> user = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectSomeThirdPartyBindPhone", param);
		manage.commit();
		if (user.size() == 1) {
			return user.get(0);
		} else {
			return null;
		}
	}
	/**
	 * select top 10 * from Ymt_MobilePhoneAccount order by dAddTime desc
	 * @return
	 */
	public List<Map> selectLastPhoneAccount() {
		List<Map> user = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectLastPhoneAccount", null);
		manage.commit();
		return user;
	}
	public List<Map> selectUserinfoUpdaterecord(int uid) {
		Map param = new HashMap<String, Object>();
		param.put("uid", uid);
		List<Map> user = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectUserinfoUpdaterecord", param);
		manage.commit();
		return user;
	}
	
	/**
	 * 
	 * @param queryField 查询的字段
	 * @param joinCondition 连表查询
	 * @param queryCondition 查询条件
	 * @return
	 */
	public List<Map> selectUsers(String queryField, String joinCondition, String queryCondition) {
		Map m = new HashMap();
		if(null != queryField && !"".equals(queryField)){
			m.put("queryField", queryField);
		}
		if(null != joinCondition && !"".equals(joinCondition)){
			m.put("joinCondition", joinCondition);
		}
		if(null != queryCondition && !"".equals(queryCondition)){
			m.put("queryCondition", queryCondition);
		}
		return (List<Map>) manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectUsers", m);
	}
	
	
	public YmtUsers selectByToken(String token) {
		YmtUsers ymtUsers=new YmtUsers();
		Map param = new HashMap<String, Object>();
		param.put("token", token);
		List<Map> user = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtUsersMapper.selectByToken", param);
		if(user!=null && user.size()>0)
		{
			ymtUsers=(YmtUsers)user.get(0);
		}
		manage.commit();
		return ymtUsers;
	}
	
	

	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String args[]) {
		try {
			Logger.debug(new YmtUsersWapper().getLoginId(3383));
			// Logger.debug(new
			// YmtUsersWapper().selectUserAtWeixinByUserId(1113));

		} catch (Exception e) {
			e.printStackTrace();
		}
		// Logger.debug(new YmtUsersWapper().getLoginId(3383));
	}
}
